/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 07/04/2006
 * Time: 12:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using Orz.LogManager.Config;

namespace Orz.LogManager
{
	/// <summary>
	/// Description of LoggingFormFactory.
	/// </summary>
	public class LoggingFormFactory
	{
		public LoggingFormFactory()
		{
		}
		
		
		public static LoggingBaseForm CreateForm(logsetLogfile file_p, 
												 logset configFile_p)
		{
			
			//=================================================================
			// bit of a nasty one - if you don't specify any autologin parameters then
			// this array will be null.
			//=================================================================
			if (null == file_p.autoLogin)
			{
				file_p.autoLogin = new logsetLogfileAutoLogin[0];
			}
			
			string sName = file_p.name;						
			
			
			//=================================================================
			// Now create the stream, depending on the type specified in the
			// logfile config - either Local, Telnet, or SSH.
			//=================================================================
			LoggingBaseForm form = null;
			ILogManagerStream stream = null;
			
			if (LogsetLoader.isTelnet(file_p))
			{							
				stream = new TelnetFileStream(file_p.telnetHost, file_p.telnetCommand, FileFinder.ConvertToUnix(file_p.path), file_p.autoLogin);			
				System.Threading.Thread.Sleep(500);
				form = new TelnetLoggingForm(sName, stream);
			}
			else if (LogsetLoader.isSSH(file_p))
			{					
				string sPassword = PasswordHelper.GetSSHPassword (file_p);
				
				// If the user pressed cancel just return here - don't open it.
				if (null == sPassword) return null;

				stream = new SshFileStream(file_p.telnetHost, file_p.sshUsername, sPassword, file_p.telnetCommand, FileFinder.ConvertToUnix(file_p.path), file_p.autoLogin);
				System.Threading.Thread.Sleep(500);
				form = new SshLoggingForm(sName, stream);
			}
			else
			{
				stream = new LocalFileStream (FileFinder.FindFile (file_p.path));
				form = new LocalLoggingForm(sName, stream);
			}
			
			
			
			//=================================================================
			// Associate the new input stream with the MDI child form
			//=================================================================
			    
			form.UnixEncoding = (file_p.encoding == encodingType.unix);
			form.MaxCharsPerWindow = System.Int32.Parse(configFile_p.maxCharsPerWindow);								
			
			
			//=================================================================
			// Also add any line-matching conditions
			//=================================================================
			if (null != file_p.match)
			{
				foreach (logsetLogfileMatch oFileMatch in file_p.match)
				{
					LineMatch oMatch = new LineMatch(oFileMatch.pattern, oFileMatch.colour, oFileMatch.replacement);
					form.AddLineMatch(oMatch);
				}
			}
						
			
			return form;
		}
	}
}
